/*
 * AgentsFragment.java
 * business
 *
 * Created by SunDing on 2016/9/12.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.agents;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.ui.adapter.AgentsAdapter;
import com.yidiandao.business.ui.agents.agent.AgentGoodsFragment;
import com.yidiandao.business.ui.agents.agent.AgentGoodsPresenter;
import com.yidiandao.business.ui.agents.delivery.DeliveryFragment;
import com.yidiandao.business.ui.agents.delivery.DeliveryPresenter;

import java.util.List;

/**
 * 我是代理商
 */
public class AgentsFragment extends BaseFragment implements AgentsContract.View {

    private Toolbar mToolbar;

    private ViewPager mViewPager;

    private TabLayout mTabLayout;

    private AgentsContract.Presenter mPresenter;
    private AgentGoodsFragment mAgentGoodsFragment;
    private DeliveryFragment mDeliveryFragment;
    private AgentsAdapter mAgentsAdapter;

    public static AgentsFragment getInstance(Bundle extras) {

        AgentsFragment fragment = new AgentsFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (null != savedInstanceState) {
            mAgentsAdapter = new AgentsAdapter(getChildFragmentManager());
            List<Fragment> fragments = getChildFragmentManager().getFragments();
            for (Fragment fragment : fragments) {
                if (fragment instanceof AgentGoodsFragment) {
                    mAgentGoodsFragment = (AgentGoodsFragment) fragment;
                } else if (fragment instanceof DeliveryFragment) {
                    mDeliveryFragment = (DeliveryFragment) fragment;
                }
            }
            addFragments();
        }
    }

    @Override
    public void setPresenter(AgentsContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return AgentGoodsFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    protected void initViews() {
        mTabLayout = (TabLayout) mRootView.findViewById(R.id.tab_layout);
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mViewPager = (ViewPager) mRootView.findViewById(R.id.viewpager_delivery);

        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
            }
        });

        initData();
    }

    private void initData() {
        if (null == mAgentsAdapter) {
            mAgentsAdapter = new AgentsAdapter(getChildFragmentManager());
            mAgentGoodsFragment = AgentGoodsFragment.getInstance();
            mDeliveryFragment = DeliveryFragment.getInstance();
            addFragments();
        }
        mViewPager.setAdapter(mAgentsAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        new AgentGoodsPresenter(mAgentGoodsFragment);
        new DeliveryPresenter(mDeliveryFragment);

        boolean toAgentOrderFragment =
                getArguments().getBoolean(Intents.EXTRA_TO_AGENT_ORDER_FRAGMENT, false);
        mViewPager.setCurrentItem(toAgentOrderFragment ? 1 : 0);
    }

    private void addFragments() {
        mAgentsAdapter.addFragment(mAgentGoodsFragment);
        mAgentsAdapter.addFragment(mDeliveryFragment);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_agents;
    }
}
